﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using TopBuilds.Restaurant.Data;
using TopBuilds.Restaurant.ViewModel;

namespace TopBuilds.Restaurant.ManagementClient
{
    /// <summary>
    /// PrivilegeManagement.xaml 的交互逻辑
    /// </summary>
    public partial class PrivilegeManagement : Page
    {
        public PrivilegeManagement()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, RoutedEventArgs e) //增加
        {
            string name = nametext.Text;
            //string attribute = attributetext.Text;

            if (name != null && name.Length > 0 )
            {
                Privilege privilege = new Privilege(name);

                if (PrivilegeObject.Add(privilege) == -1)
                {
                    MessageBox.Show("增加失败");
                    return;
                }

                #region re-binding
                //listBox1.ItemsSource = null;
                //listBox1.ItemsSource = t;

                // re-bind for TextBox
                Binding Name_binding = new Binding("Name");
                Name_binding.Mode = BindingMode.OneWay;
                nametext.SetBinding(TextBox.TextProperty, Name_binding);

                /*Binding Attribute_binding = new Binding("Attribute");
                Attribute_binding.Mode = BindingMode.OneWay;
                attributetext.SetBinding(TextBox.TextProperty, Attribute_binding);*/
                #endregion

                /// reset the data from database
                PrivilegeCollection t = listBox1.ItemsSource as PrivilegeCollection;
                t.Add(privilege);

                MessageBox.Show("增加成功");
            }
        }

        private void buttonUpdate_Click(object sender, RoutedEventArgs e) //更新
        {
            int selectedIndex = listBox1.SelectedIndex;
            if (selectedIndex >= 0)
            {
                PrivilegeCollection t = listBox1.ItemsSource as PrivilegeCollection;

                Privilege privilege = t[selectedIndex] as Privilege;
                privilege.Name = nametext.Text;
                //privilege.Attribute = attributetext.Text;

                #region re-binding

                // re-binding for ListBox
                listBox1.ItemsSource = null;
                listBox1.ItemsSource = t;

                // re-bind for TextBox
                Binding Name_binding = new Binding("Name");
                Name_binding.Mode = BindingMode.OneWay;
                nametext.SetBinding(TextBox.TextProperty, Name_binding);

                /*Binding Attribute_binding = new Binding("Attribute");
                Attribute_binding.Mode = BindingMode.OneWay;
                attributetext.SetBinding(TextBox.TextProperty, Attribute_binding);*/

                #endregion

                if (PrivilegeObject.Update(privilege) == -1)
                {
                    MessageBox.Show("更新失败");
                }
            }
        }
            

        private void buttonDelete_Click(object sender, RoutedEventArgs e)  //删除
        {
            int selectedIndex = listBox1.SelectedIndex;
            if (selectedIndex >= 0)
            {
                PrivilegeCollection t = listBox1.ItemsSource as PrivilegeCollection;
                int id = (t[selectedIndex] as Privilege).Id;

                if (MessageBoxResult.Yes == MessageBox.Show("确定要删除么?", "警告", MessageBoxButton.YesNo))
                {
                    t.RemoveAt(selectedIndex);

                    #region re-binding
                    // re-bound for ListBox
                    //listBox1.ItemsSource = null;
                    //listBox1.ItemsSource = t;

                    // re-binding for TextBox
                    Binding Name_binding = new Binding("Name");
                    Name_binding.Mode = BindingMode.OneWay;
                    nametext.SetBinding(TextBox.TextProperty, Name_binding);

                    /*Binding Attribute_binding = new Binding("Attribute");
                    Attribute_binding.Mode = BindingMode.OneWay;
                    attributetext.SetBinding(TextBox.TextProperty, Attribute_binding);*/
                    #endregion

                    if ( ! PrivilegeObject.Delete(id))
                    {
                        MessageBox.Show("删除失败");
                    }
                }
            }
        }
    }
}
